package com.kelvin.paopao.module.main.date.dateperson;


import com.kelvin.kelvinlib.utils.ToastUtil;
import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.DatePersonModel;
import com.kelvin.paopao.utils.DialogMaker;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by MCK on 2017/5/17.
 */

public class DatePersonPresenter implements IDatePersonPresenter {

    private DatePersonActivity mView;

    public DatePersonPresenter(DatePersonActivity view) {
        this.mView = view;
    }

    @Override
    public void getData(int id) {
        RetrofitService.getDatePerson(String.valueOf(id)).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                mView.showLoading();
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse<DatePersonModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<DatePersonModel> commonListResponse)
                            throws Exception {
                        mView.hideLoading();
                        if (commonListResponse.getResult().size() == 0) {
                            mView.loadEmpty();
                        } else {
                            mView.loadData(commonListResponse.getResult());
                            boolean isCanCheck = true;
                            for (DatePersonModel model : commonListResponse.getResult()) {
                                if (model.getStates() == 1) {
                                    isCanCheck = false;
                                }
                            }
                            mView.loadCheck(isCanCheck);
                        }


                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        mView.hideLoading();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void confirm(String id, String user_no) {
        RetrofitService.confirm(id, user_no).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                DialogMaker.showProgressDialog(mView, "确认中~~");
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse>() {
                    @Override
                    public void accept(@NonNull CommonListResponse commonListResponse) throws Exception {
                        DialogMaker.diamiss();
                        if (commonListResponse.getReason().equals("成功")) {
                            ToastUtil.showToast(mView, "选择成功，现在开始你们的约会吧~~");
                            mView.finish();
                        } else {
                            ToastUtil.showToast(mView, commonListResponse.getReason());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        DialogMaker.diamiss();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        DialogMaker.diamiss();
                    }
                });
    }

    @Override
    public void getData(boolean isRefresh) {

    }

    @Override
    public void getMoreData() {

    }
}
